package com.das.mechanic_base.adapter.main;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.utils.X3StringUtils;

import java.util.ArrayList;
import java.util.List;

public class X3EditAppearanceResultAdapter extends RecyclerView.Adapter<X3EditAppearanceResultAdapter.EditAppearanceResultHolder> {

    private List<String> mList;

    private Context mContext;

    private String sn;

    private List<Boolean> sList;

    public X3EditAppearanceResultAdapter(List<String> mList, Context mContext, String sn) {
        this.mList = mList;
        this.mContext = mContext;
        this.sn = sn;
        this.sList = new ArrayList<>();
        for (int i = 0; i < mList.size(); i++) {
            sList.add(false);
        }
    }

    public void changeSelectChoice(String select) {
        if (X3StringUtils.isEmpty(select)) {
            return;
        }
        if (X3StringUtils.isListEmpty(mList)) {
            return;
        }
        this.sList.clear();
        for (int i = 0; i < mList.size(); i++) {
            String replace = mList.get(i).replace("A. ", "").replace("B. ", "").replace("C. ", "").replace("D. ","").replace("E. ","");
            sList.add(select.equals(replace));
        }
        notifyDataSetChanged();
    }

    /**
     * 全部通过
     * @return
     */
    public String changeAllPass() {
        if (X3StringUtils.isListEmpty(mList)) {
            return "";
        }
        this.sList.clear();
        for (int i = 0; i < mList.size(); i++) {
            sList.add(i == 0);
        }
        notifyDataSetChanged();
        return mList.get(0);
    }

    @NonNull
    @Override
    public X3EditAppearanceResultAdapter.EditAppearanceResultHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new EditAppearanceResultHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_car_choice_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull X3EditAppearanceResultAdapter.EditAppearanceResultHolder holder, int position) {
        holder.tv_content.setText(mList.get(position));
        if (sList.get(position)) {
            String content = mList.get(position);
            if (X3StringUtils.isEmpty(content)) {
                holder.tv_content.setTextColor(Color.parseColor("#F64458"));
                holder.tv_content.setBackground(ContextCompat.getDrawable(mContext,R.drawable.x3_alone_choice_dangerous_bg));
            }else {
                String substring = content.substring(0, 1);
//                if ("A".equalsIgnoreCase(substring)) {
//                    holder.tv_content.setTextColor(Color.parseColor("#18BE6B"));
//                    holder.tv_content.setBackground(ContextCompat.getDrawable(mContext,R.drawable.alone_choice_normal_bg));
//                }else {
                    holder.tv_content.setTextColor(Color.parseColor("#F64458"));
                    holder.tv_content.setBackground(ContextCompat.getDrawable(mContext,R.drawable.x3_alone_choice_dangerous_bg));
           //     }
            }
        }else {
            holder.tv_content.setTextColor(Color.parseColor("#333333"));
            holder.tv_content.setBackground(ContextCompat.getDrawable(mContext,R.drawable.x3_alone_add_new_service_item));
        }

        holder.itemView.setOnClickListener(v -> {
            for (int i = 0; i < sList.size(); i++) {
                if (i == position) {
                    if (sList.get(i)) {
                        sList.remove(i);
                        sList.add(i,false);
                        if (iOnClickChoice != null) {
                            iOnClickChoice.iOnClickChoiceContent("",sn);
                        }
                    }else {
                        sList.remove(i);
                        sList.add(i,true);
                        if (iOnClickChoice != null) {
                            iOnClickChoice.iOnClickChoiceContent(holder.tv_content.getText().toString(),sn);
                        }
                    }
                }else {
                    sList.remove(i);
                    sList.add(i,false);
                }
            }
            notifyDataSetChanged();


        });
    }

    X3CarChoiceAdapter.IOnClickChoice iOnClickChoice;

    public void setiOnClickChoice(X3CarChoiceAdapter.IOnClickChoice iOnClickChoice) {
        this.iOnClickChoice = iOnClickChoice;
    }

    public interface IOnClickChoice {
        void iOnClickChoiceContent(String content,String sn);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class EditAppearanceResultHolder extends RecyclerView.ViewHolder {
        TextView tv_content;
        public EditAppearanceResultHolder(@NonNull View itemView) {
            super(itemView);
            tv_content = itemView.findViewById(R.id.tv_content);
        }
    }
}
